//package dsk.demo.product;
//
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.http.HttpUtil;
//import cn.hutool.json.JSONArray;
//import cn.hutool.json.JSONNull;
//import cn.hutool.json.JSONObject;
//import cn.hutool.json.JSONUtil;
//import cn.hutool.poi.excel.ExcelUtil;
//import cn.hutool.poi.excel.ExcelWriter;
//import dsk.demo.product.entity.CustomerEntity;
//import dsk.demo.product.entity.CustomerLinkManEntity;
//import org.junit.jupiter.api.Test;
//
//import java.util.*;
//
//public class 海关数据爬取 {
//
//    private static ExcelWriter companyWriter = ExcelUtil.getWriter("C:\\Users\\Administrator\\Desktop\\采购商_希腊_公司.xls");
//    private static ExcelWriter contactWriter = ExcelUtil.getWriter("C:\\Users\\Administrator\\Desktop\\采购商_希腊_联系人.xls");
//    private static ExcelWriter contactProWriter = ExcelUtil.getWriter("C:\\Users\\Administrator\\Desktop\\采购商_希腊_联系人_问题.xls");
//    private static Integer countryId = 80;
//    private static String Authorization = "9598ee6f7004671924aaee751eac9eba";
//
//    static {
//        LinkedHashMap<String, String> map = new LinkedHashMap<>();
//        map.put("country","国家");
//        map.put("companyName","公司名称");
//        map.put("id","公司ID");
//        map.put("hsListStr","海关编号");
//        companyWriter.setHeaderAlias(map);
//        //只写入配置了别名的属性
//        companyWriter.setOnlyAlias(true);
//
//        LinkedHashMap<String, String> refMap = new LinkedHashMap<>();
//        refMap.put("area","国家");
//        refMap.put("name","姓名");
//        refMap.put("email","邮箱");
//        refMap.put("phone","电话");
//        refMap.put("companyId","公司ID");
//        contactWriter.setHeaderAlias(refMap);
//        //只写入配置了别名的属性
//        contactWriter.setOnlyAlias(true);
//
//
//        contactProWriter.setHeaderAlias(refMap);
//        //只写入配置了别名的属性
//        contactProWriter.setOnlyAlias(true);
//
//    }
//
//    @Test
//    public void 海关数据爬取主函数() {
//        int index = 1;
//        int maxPage = 1;
//        while (true) {
//            if (maxPage < index) {
//                break;
//            }
//            //获取某个国家从中国采购的 采购商ID列表
//            Map<String,Object> jsonParams = new HashMap<>();
//            jsonParams.put("countryId",countryId);
//            jsonParams.put("buyFromArea",0);
//            jsonParams.put("excludeBank",true);
//            jsonParams.put("excludeLogistics",true);
//            jsonParams.put("keyword","");
//            jsonParams.put("pageIndex",index);
//            jsonParams.put("pageSize",10);
//            jsonParams.put("personalSort",false);
//            jsonParams.put("productDescription",new ArrayList<>());
//            jsonParams.put("searchId","12286951");
//            jsonParams.put("searchType",null);
//            jsonParams.put("sort","intelligent");
//            jsonParams.put("sortType","desc");
//            jsonParams.put("showContactIcon",true);
//            jsonParams.put("tradeDate",1);
//            jsonParams.put("type",1);
//            String s = JSONUtil.toJsonStr(jsonParams);
//            String content = HttpUtil.createPost("https://et.topease.net/api/gt/search/search")
//                    .header("Authorization",Authorization)
//                    .header("Host","et.topease.net")
//                    .header("Origin","https://et.topease.net")
////                .header("Referer","https://et.topease.net/gt5/company/result?c=46&sort=intelligent")
//                    .header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0")
//                    .header("Pragma","no-cache")
//                    .header("Connection","keep-alive")
//                    .header("Cache-Control","no-cache")
//                    .header("Accept-Encoding","gzip, deflate, br")
//                    .header("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2")
//                    .header("Sec-Fetch-Dest","empty")
//                    .header("Sec-Fetch-Mode","cors")
//                    .header("Sec-Fetch-Site","same-origin")
//                    .header("Content-Type","application/json")
//                    .header("Accept","application/json, text/plain, */*")
//                    .header("website","https://et.topease.net")
//                    .body(s)
//                    .execute().body();
//            Map result = JSONUtil.toBean(content,Map.class);
//            if (index == 1) {
//                maxPage = ((int)((Map) result.get("data")).get("total"))/10 + 1;
//            }
//            JSONArray hsList = (JSONArray)((Map) result.get("data")).get("result");
//            if (hsList.size() > 0) {
//                for (Object o : hsList) {
//                    JSONObject obj = (JSONObject) o;
//                    String country = ((String)obj.get("country"));
//                    String companyName = ((String)obj.get("companyName"));
//                    String id = ((String)obj.get("id"));
//                    String hsListStr = getHSCodeList(id);
//                    //写入一行
//                    CustomerEntity customer = new CustomerEntity();
//                    customer.setId(id);
//                    customer.setCountry(country);
//                    customer.setCompanyName(companyName);
//                    customer.setHsListStr(hsListStr);
//                    System.out.println(customer);
//
//                    companyWriter.writeRow(customer,false);
//                    companyWriter.flush();
//                    //获取该公司联系方式
//                    getContactList(id);
//                }
//            }
//            index++;
//        }
//
//    }
//
//
//
//    public String getHSCodeList(String buyerId){
//        //从供应商ID  获取它的前10采购的  海关编号
//        Map<String,Object> jsonParams = new HashMap<>();
//
//        List<Map> companyIds = new ArrayList<>();
//        HashMap<String, Object> company = new HashMap<>();
//        company.put("companyId",buyerId);
//        company.put("mergeCompanyIds",new ArrayList<>());
//        companyIds.add(company);
//        jsonParams.put("companyIds",companyIds);
//
//        jsonParams.put("countryId",countryId);
//        jsonParams.put("buyFromArea",0);
//        jsonParams.put("excludeBank",true);
//        jsonParams.put("excludeLogistics",true);
//        jsonParams.put("keyword","");
//        jsonParams.put("pageIndex",1);
//        jsonParams.put("pageSize",10);
//        jsonParams.put("personalSort",false);
//        jsonParams.put("productDescription",new ArrayList<>());
//        jsonParams.put("searchType",null);
//        jsonParams.put("showContactIcon",true);
//        jsonParams.put("sort","intelligent");
//        jsonParams.put("sortType","desc");
//        jsonParams.put("tradeDate",1);
//        jsonParams.put("type",1);
//        String s = JSONUtil.toJsonStr(jsonParams);
//        String content = HttpUtil.createPost("https://et.topease.net/api/gt/search/getHsCodeTableByCompany")
//                .header("Authorization",Authorization)
//                .header("Host","et.topease.net")
//                .header("Origin","https://et.topease.net")
////                .header("Referer","https://et.topease.net/gt5/company/result?c=46&sort=intelligent")
//                .header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0")
//                .header("Pragma","no-cache")
//                .header("Connection","keep-alive")
//                .header("Cache-Control","no-cache")
//                .header("Accept-Encoding","gzip, deflate, br")
//                .header("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2")
//                .header("Sec-Fetch-Dest","empty")
//                .header("Sec-Fetch-Mode","cors")
//                .header("Sec-Fetch-Site","same-origin")
//                .header("Content-Type","application/json")
//                .header("Accept","application/json, text/plain, */*")
//                .header("website","https://et.topease.net")
//                .body(s)
//                .execute().body();
//        Map result = JSONUtil.toBean(content,Map.class);
//        JSONArray hsList = (JSONArray)((Map) result.get("data")).get("result");
//        String returnStr = "";
//        if (hsList.size() > 0) {
//            for (Object o : hsList) {
//                JSONObject obj = (JSONObject) o;
//                String hsCode = (String) obj.get("hsCode");
//                returnStr += hsCode.substring(0,hsCode.length() > 8 ? 8 : hsCode.length()) + " ";
//            }
//        }
//
//        return returnStr;
//    }
//
//    public void getContactList(String buyerId) {
//
//        Map<String,Object> jsonParams = new HashMap<>();
//        jsonParams.put("billSearch",new Object());
//        jsonParams.put("countryId",0);
//        jsonParams.put("dataSources",new Integer[]{25,2500});
//        jsonParams.put("dateRange",new String[]{"2015-01-13","2023-3-18"});
//        jsonParams.put("id",buyerId);
//        jsonParams.put("isMerge",false);
//        jsonParams.put("onlyIe",1);
//        jsonParams.put("pageIndex",1);
//        jsonParams.put("pageSize",10);
//        jsonParams.put("tradeType",0);
//        jsonParams.put("type","1");
//        String s = JSONUtil.toJsonStr(jsonParams);
//        String content = HttpUtil.createPost("https://et.topease.net/api/gt/search/getCompanyContacts")
//                .header("Authorization",Authorization)
//                .header("Host","et.topease.net")
//                .header("Origin","https://et.topease.net")
////                .header("Referer","https://et.topease.net/gt5/company/result?c=46&sort=intelligent")
//                .header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0")
//                .header("Pragma","no-cache")
//                .header("Connection","keep-alive")
//                .header("Cache-Control","no-cache")
//                .header("Accept-Encoding","gzip, deflate, br")
//                .header("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2")
//                .header("Sec-Fetch-Dest","empty")
//                .header("Sec-Fetch-Mode","cors")
//                .header("Sec-Fetch-Site","same-origin")
//                .header("Content-Type","application/json")
//                .header("Accept","application/json, text/plain, */*")
//                .header("website","https://et.topease.net")
//                .body(s)
//                .execute().body();
//
//        Map result = JSONUtil.toBean(content,Map.class);
//        Object data = result.get("data");
//        JSONArray list = null;
//        if (data instanceof Map) {
//            list = (JSONArray)((Map) data).get("result");
//        } else {
//            return;
//        }
//        List<CustomerLinkManEntity> resultList = new ArrayList<>();
//        List<CustomerLinkManEntity> resultListProblem = new ArrayList<>();
//
//        if (list.size() > 0) {
//            for (Object o : list) {
//                JSONObject obj = (JSONObject) o;
//                String email = getByJsonObj(obj,"email");
//                String name = getByJsonObj(obj,"name");
//                String country = getByJsonObj(obj,"country");
////                String address = ((String)obj.get("address"));
//                String tel = getByJsonObj(obj,"tel");
//                CustomerLinkManEntity customer = new CustomerLinkManEntity();
//                customer.setArea(country);
//                customer.setName(name);
//                customer.setEmail(email);
//                customer.setPhone(tel);
//                customer.setCompanyId(buyerId);
//                if (email.indexOf("@") == email.lastIndexOf("@")) {
//                    resultList.add(customer);
//                } else {
//                    resultListProblem.add(customer);
//                }
//            }
//        }
//        // 写入 resultList;
//        contactWriter.write(resultList,false);
//        contactWriter.flush();
//        contactProWriter.write(resultListProblem,false);
//        contactProWriter.flush();
//    }
//
//    public String getByJsonObj(JSONObject obj,String key) {
//        Object o = obj.get(key);
//        if (o instanceof JSONNull) {
//            return "";
//        } else {
//            if (StrUtil.isBlank((String)o)) {
//                return "";
//            }
//            return (String)o;
//        }
//    }
//
//    public boolean getCountOfChar(String str,String ch) {
//        return str.indexOf(ch) == str.lastIndexOf(ch);
//    }
//
//}
